package cn.xiaopengstack.infrastructure.repository;

import cn.xiaopengstack.domain.order.model.aggregate.CreateOrderAggregate;
import cn.xiaopengstack.domain.order.model.entity.OrderEntity;
import cn.xiaopengstack.domain.order.model.entity.PayOrderEntity;
import cn.xiaopengstack.domain.order.model.entity.ProductEntity;
import cn.xiaopengstack.domain.order.model.entity.ShopCarEntity;
import cn.xiaopengstack.domain.order.model.valobj.PayOrderStatusEnumVO;
import cn.xiaopengstack.domain.order.repository.IOrderRepository;
import cn.xiaopengstack.infrastructure.event.EventPublish;
import cn.xiaopengstack.infrastructure.repository.dao.IPayOrderDao;
import cn.xiaopengstack.infrastructure.repository.po.PayOrder;
import cn.xiaopengstack.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author jiangyangang
 */
@Repository
@Slf4j
public class OrderRepository implements IOrderRepository {

    @Resource
    private IPayOrderDao payOrderDao;

    @Resource
    private EventPublish eventPublish;

    @Override
    public void createOrder(CreateOrderAggregate createOrderAggregate) {
        OrderEntity orderEntity = createOrderAggregate.getOrderEntity();
        ProductEntity productEntity = createOrderAggregate.getProductEntity();
        ShopCarEntity shopCarEntity = createOrderAggregate.getShopCarEntity();

        PayOrder payOrder  = PayOrder.builder()
                .orderTime(new Date())
                .createTime(new Date())
                .totalAmount(productEntity.getPrice())
                .status(PayOrderStatusEnumVO.create.getStatus())
                .userId(shopCarEntity.getUserId())
                .orderId(orderEntity.getOrderId())
                .productId(productEntity.getProductId())
                .productName(productEntity.getProductName())
                .build();
        payOrderDao.insertSelective(payOrder);
    }

    @Override
    public OrderEntity queryUnPayOrder(ShopCarEntity shopCarEntity) {

        PayOrder payOrder = payOrderDao.selectUnpayOrder(shopCarEntity.getProductId(), shopCarEntity.getUserId());
        if (Objects.nonNull(payOrder)) {
            return OrderEntity.builder()
                    .orderTime(payOrder.getOrderTime())
                    .payUrl(payOrder.getPayUrl())
                    .productName(payOrder.getProductName())
                    .totalAmount(payOrder.getTotalAmount())
                    .orderId(payOrder.getOrderId())
                    .productId(payOrder.getProductId())
                    .orderStatusVO(PayOrderStatusEnumVO.valueOf(payOrder.getStatus()))
                    .build();
        }

        return null;
    }

    @Override
    public OrderEntity queryPayOrder(String orderId) {
        return null;
    }

    @Override
    public void updatePayOrder(PayOrderEntity payOrderEntity) {
        PayOrder payOrder = new PayOrder();
        payOrder.setOrderId(payOrderEntity.getOrderId());
        payOrder.setPayUrl(payOrderEntity.getPayUrl());
        payOrder.setStatus(payOrderEntity.getOrderStatus().getStatus());
        payOrder.setUpdateTime(new Date());
        payOrder.setPayTime(payOrderEntity.getPayTime());
        payOrderDao.updateByOrderIdSelective(payOrder);
    }

    @Override
    public void sendOrderPayResultMsg(BaseEvent<?> event) {
        eventPublish.publish(event);
    }
}
